﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebCrawler.HtmlParser.Interfaces;
using WebCrawler.HttpConnection.Interfaces;
using WebCrawler.HttpConnection;
using HTMLParser.Model;
using GlobalInterfaces;

namespace WebCrawler.HtmlParser
{
    public class HtmlDoc : IHtmlDoc //, IObservable, IObserver
    {
        private static IList<IObserver> _observers = new List<IObserver>();
        private IHttp _request;
        private IHtmlParser _parser;
        private IList<string> _links;
        public IList<string> AllLinks { get { return _links; } }
        public Uri URI { get; set; } // test
        public string Url { get; set; }
        public string Title { get; protected set; }

        public HtmlDoc(string url)
        {
            _request = new HttpRequest(url);
            this.Url = url;
        }
        public HtmlDoc(string url, IList<IObserver> os)
            : this(url)
        {
            _observers = os;
            _request = new HttpRequest(url, _observers);
        }

        public IList<string> GetAllLinks()
        {
            try
            {
                if (_links == null)
                {
                    _parser = new RegexParser(_request.ServerUrl, _request.PathUrl, _request.Content);
                    _links = _parser.GetHrefs();
                    _links = _links.Distinct().ToList();
                }
            }
            catch (Exception ex)
            {
                //Update(ex.Message);
            }
            return _links;
        }

        //public void Update(string msg)
        //{ NotifyAll(msg); }
        //public void NotifyAll(string msg)
        //{
        //    foreach (IObserver o in _observers)
        //    {
        //        o.Update(msg);
        //    }
        //}
        


        
    }
}
